package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.organization.util.IssueFeePolicy;
import com.turing.post.organization.util.IssueFeePolicyDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Mar 12, 2013
 */
@Stateless(mappedName = "organization.impl.IssueFeePolicyDaoImpl")
@Remote
@Local
public class IssueFeePolicyDaoImpl implements IssueFeePolicyDao {

	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	/**
	 * 查询满足条件的资费策略
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<IssueFeePolicy> getIssueFeePolicyByBusiness(IssueFeePolicy ifp) {
		Query query = _em
				.createNativeQuery(
						"select t.* from IssueFeePolicy t where "
								+ "t.IssueCompanyNo = ?1 and t.Status in ( '1', '2' ) "
								+ "and (t.BusinessType = ?2) "
								+ "and (t.SaleBranchNo = ?3 or t.SaleBranchNo is null) "
								+ "and (t.CardFaceId = ?4 or t.CardFaceId is null) "
								+ "and (t.CardClassicId = ?5 or t.CardClassicId is null) "
								+ "and (t.ToCardClassicId = ?6 or t.ToCardClassicId is null) "
								+ "and (t.StartCardSeqNo <= ?7 or t.StartCardSeqNo is null) "
								+ "and (t.EndCardSeqNo >= ?7 or t.EndCardSeqNo is null) "
								+ "and (t.StartDate <= to_date( ?8, 'yyyy-MM-dd' ) or t.StartDate is null) "
								+ "and (t.EndDate >= to_date( ?8, 'yyyy-MM-dd' ) or t.EndDate is null) "
								+ "and (t.StartAmount <= ?9 or t.StartAmount = 0 or t.StartAmount is null) "
								+ "and (t.EndAmount >= ?9 or t.EndAmount = 0 or t.EndAmount is null) "
								+ "order by t.AuthTime desc",
						IssueFeePolicy.class);

		query.setParameter(1, ifp.getIssueCompanyNo());
		query.setParameter(2, ifp.getBusinessType());
		query.setParameter(3, ifp.getSaleBranchNo());
		query.setParameter(4, ifp.getCardFaceId());
		query.setParameter(5, ifp.getCardClassicId());
		query.setParameter(6, ifp.getToCardClassicId());
		query.setParameter(7, ifp.getStartCardSeqNo());
		query.setParameter(8, ifp.getStartDate());
		query.setParameter(9, ifp.getStartAmount());

		return query.getResultList();
	}

	@Override
	public void delete(BigInteger issueFeePolicy)
			throws PCPSApplicationException {
		GeneralDao.remove(_em, getIssueFeePolicyByFeePolicyId(issueFeePolicy));
	}

	@Override
	public IssueFeePolicy getIssueFeePolicyByFeePolicyId(BigInteger feePolicyId) {
		return _em.find(IssueFeePolicy.class, feePolicyId);
	}

	// @Override
	// public List<IssueFeePolicy> getIssueFeePolicyListByCond(
	// String issueCompanyNo, String businessType,
	// BigInteger cardClassicId, BigInteger toCardClassicId,
	// String startCardSeqNo, String endCardSeqNo, String startDate,
	// String endDate, String amountType, BigInteger cardFaceId,
	// Integer page) {
	// // String sql="select * from IssueFeePolicy
	// // where IssueCompanyNo=?1 and BusinessType=?2 and (CardClassicId is
	// // null or CardClassicId=?3)
	// // and ((?4 is null and ToCardClassicId is null) or ToCardClassicId=?4)
	// // and (StartCardSeqNo is null or StartCardSeqNo<=?5)
	// // and (EndCardSeqNo is null or EndCardSeqNo>=?5) and (StartDate is null
	// // or StartDate<=to_date(?6,'yyyy-mm-dd'))
	// // and (EndDate is null or EndDate>=to_date(?6,'yyyy-mm-dd'))
	// // and ((AmountType in ('0','2') and ?7='0') or (AmountType in ('1','2')
	// // and ?7='1'))
	// // and (CardFaceId is null or CardFaceId=?8)
	// // and Status='1' order by AuthTime desc";
	// return null;
	// }

	@SuppressWarnings("unchecked")
	@Override
	public List<IssueFeePolicy> getIssueFeePolicyList(PCPSWebInput webInput,
			String businessType, BigInteger cardFaceId,
			BigInteger cardClassicId, String FeeType, String Status,
			Integer page) {
		String sql = "select * from IssueFeePolicy t where 1=1";
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			return null;
		}
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		sql = sql + " and issueCompanyNo='" + issueCompanyNo + "'";
		sql = sql
				+ " and (?1 is null or businessType=?1)"
				+ " and (?2=-1 or cardFaceId =?2) and (?3=-1 or cardClassicId =?3) and "
				+ "(?4 is null or FeeType =?4)";
		if (!PCPSUtil.isEmpty(Status)&&IssueFeePolicy.IFPolicy_Wait_Auth.equals(Status)) {
			sql += "and (Status ='"
					+ IssueFeePolicy.IFPolicy_status_InvalidWait
					+ "' or Status='"
					+ IssueFeePolicy.IFPolicy_status_EffectiveWait + "')";
		} else {
			if (!PCPSUtil.isEmpty(Status)) {
				sql += " and Status ='" + Status + "'";
			}
		}
		sql += " order by t.CreateTime desc";
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery(sql, IssueFeePolicy.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		query.setParameter(1, businessType);
		query.setParameter(2, cardFaceId);
		query.setParameter(3, cardClassicId);
		query.setParameter(4, FeeType);
		return query.getResultList();
	}

	@Override
	public BigInteger getIssueFeePolicyTotal(PCPSWebInput webInput,
			String businessType, BigInteger cardFaceId,
			BigInteger cardClassicId, String FeeType, String Status) {
		String sql = "select nvl(count(*),0) from IssueFeePolicy t where 1=1";
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			return BigInteger.ZERO;
		}
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		sql = sql + " and issueCompanyNo='" + issueCompanyNo + "'";
		sql = sql
				+ " and (?1 is null or businessType=?1)"
				+ " and (?2=-1 or cardFaceId =?2) and (?3=-1 or cardClassicId =?3) and "
				+ "(?4 is null or FeeType =?4)";
		if (!PCPSUtil.isEmpty(Status)&&Status.equals(IssueFeePolicy.IFPolicy_Wait_Auth)) {
			sql += "and (Status ='"
					+ IssueFeePolicy.IFPolicy_status_InvalidWait
					+ "' or Status='"
					+ IssueFeePolicy.IFPolicy_status_EffectiveWait + "')";
		} else {
			if (!PCPSUtil.isEmpty(Status)) {
				sql += " and Status ='" + Status + "'";
			}
		}
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, businessType);
		query.setParameter(2, cardFaceId);
		query.setParameter(3, cardClassicId);
		query.setParameter(4, FeeType);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void insert(IssueFeePolicy issueFeePolicy)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, issueFeePolicy);
	}

	@Override
	public void update(IssueFeePolicy issueFeePolicy)
			throws PCPSApplicationException {
		GeneralDao.update(_em, issueFeePolicy);
	}

	@Override
	public Integer getIssueFeePolicyCountByIFP(IssueFeePolicy ifp) {
		String sql = "select nvl(count(*),0) from issuefeepolicy t"
				+ " where t.businesstype = ?1"
				+ " and (?2 is null or t.salebranchno =?2)"
				+ " and (?3 is null or t.cardfaceid = ?3)"
				+ " and (?4 is null or t.cardclassicid = ?4)"
				+ " and (?5 is null or t.tocardclassicid = ?5)"
				+ " and (?6 is null or t.startcardseqno = ?6)"
				+ " and (?7 is null or t.endcardseqno = ?7)"
				+ " and (?8 is null or t.startdate = ?8)"
				+ " and (?9 is null or t.enddate = ?9)"
				+ " and t.startamount = ?10 and t.endamount = ?11"
				+ " and t.issuecompanyno = ?12 and t.status in('1','2','3')";

		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, ifp.getBusinessType());
		query.setParameter(2, ifp.getSaleBranchNo());
		query.setParameter(3, ifp.getCardFaceId());
		query.setParameter(4, ifp.getCardClassicId());
		query.setParameter(5, ifp.getToCardClassicId());
		query.setParameter(6, ifp.getStartCardSeqNo());
		query.setParameter(7, ifp.getEndCardSeqNo());
		query.setParameter(8, ifp.getStartDate());
		query.setParameter(9, ifp.getEndDate());
		query.setParameter(10, ifp.getStartAmount());
		query.setParameter(11, ifp.getEndAmount());
		query.setParameter(12, ifp.getIssueCompanyNo());
		return Integer.valueOf(query.getSingleResult().toString());
	}
}
